package io.xuan.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xuan.common.exception.LinfengException;
import io.xuan.common.utils.PageUtils;
import io.xuan.common.utils.Query;
import io.xuan.modules.app.dao.CarLineDao;
import io.xuan.modules.app.dao.CarStationDao;
import io.xuan.modules.app.entity.CarLineEntity;
import io.xuan.modules.app.service.CarLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("carLineService")
public class CarLineServiceImpl extends ServiceImpl<CarLineDao, CarLineEntity> implements CarLineService {

    @Autowired
    private CarLineDao carLineDao;

    @Autowired
    private CarStationDao carStationDao;

    public String LineHandle(String name) {
        if (name != null) {
            String Lat = carStationDao.searchLat(name);
            String Lng = carStationDao.searchLng(name);
            String LngLat;
            LngLat = "lng:" + Lng + ", lat:" + Lat;
            return LngLat;
        } else return "null";
    }

    public List<CarLineEntity> AllCarLine() {
        List<CarLineEntity> carLineList = carLineDao.allLine();

        for (CarLineEntity carLine : carLineList) {
            // 查询并设置 startStation 的经纬度
            String startLatLng = LineHandle(carLine.getStartStation());
            carLine.setStartStation(startLatLng);

            // 查询并设置 endStation 的经纬度
            String endLatLng = LineHandle(carLine.getEndStation());
            carLine.setEndStation(endLatLng);

            String passStation1 = LineHandle(carLine.getPassStation1());
            carLine.setPassStation1(passStation1);

            String passStation2 = LineHandle(carLine.getPassStation2());
            carLine.setPassStation2(passStation2);

            String passStation3 = LineHandle(carLine.getPassStation3());
            carLine.setPassStation3(passStation3);

            String passStation4 = LineHandle(carLine.getPassStation4());
            carLine.setPassStation4(passStation4);

            String passStation5 = LineHandle(carLine.getPassStation5());
            carLine.setPassStation5(passStation5);

            String passStation6 = LineHandle(carLine.getPassStation6());
            carLine.setPassStation6(passStation6);

            String passStation7 = LineHandle(carLine.getPassStation7());
            carLine.setPassStation7(passStation7);

            String passStation8 = LineHandle(carLine.getPassStation8());
            carLine.setPassStation8(passStation8);

            String passStation9 = LineHandle(carLine.getPassStation9());
            carLine.setPassStation9(passStation9);

            String passStation10 = LineHandle(carLine.getPassStation10());
            carLine.setPassStation10(passStation10);

        }
        return carLineList;
    }

    public List<CarLineEntity> CarLine(Integer lineId) {
        List<CarLineEntity> carLineList = carLineDao.searchLine(lineId);

        for (CarLineEntity carLine : carLineList) {
            // 查询并设置 startStation 的经纬度
            String startLatLng = LineHandle(carLine.getStartStation());
            carLine.setStartStation(startLatLng);

            // 查询并设置 endStation 的经纬度
            String endLatLng = LineHandle(carLine.getEndStation());
            carLine.setEndStation(endLatLng);

            String passStation1 = LineHandle(carLine.getPassStation1());
            carLine.setPassStation1(passStation1);

            String passStation2 = LineHandle(carLine.getPassStation2());
            carLine.setPassStation2(passStation2);

            String passStation3 = LineHandle(carLine.getPassStation3());
            carLine.setPassStation3(passStation3);

            String passStation4 = LineHandle(carLine.getPassStation4());
            carLine.setPassStation4(passStation4);

            String passStation5 = LineHandle(carLine.getPassStation5());
            carLine.setPassStation5(passStation5);

            String passStation6 = LineHandle(carLine.getPassStation6());
            carLine.setPassStation6(passStation6);

            String passStation7 = LineHandle(carLine.getPassStation7());
            carLine.setPassStation7(passStation7);

            String passStation8 = LineHandle(carLine.getPassStation8());
            carLine.setPassStation8(passStation8);

            String passStation9 = LineHandle(carLine.getPassStation9());
            carLine.setPassStation9(passStation9);

            String passStation10 = LineHandle(carLine.getPassStation10());
            carLine.setPassStation10(passStation10);

        }
        return carLineList;
    }




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarLineEntity> page = this.page(
                new Query<CarLineEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 分类保存
     *
     * @param carline
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCarLine(CarLineEntity carline) {
        Integer count = this.lambdaQuery()
                .eq(CarLineEntity::getLineId, carline.getLineId())
                .count();
        if (count > 0) {
            throw new LinfengException("分类名不能重复");
        }
        boolean save = this.save(carline);
        if (!save) {
            throw new LinfengException("分类保存失败");
        }
    }

    /**
     * 删除分类
     *
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIdList(List<Integer> list) {
        // 删除分类时，需要判断该分类是否被使用

        this.removeByIds(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCarLine(CarLineEntity carLine) {
        Integer count = this.lambdaQuery()
                .eq(CarLineEntity::getLineId, carLine.getLineId())
                .count();
        if (count > 1) {
            throw new LinfengException("名不能重复");
        }
        boolean update = this.updateById(carLine);
        if (!update) {
            throw new LinfengException("更新失败");
        }
    }


}
